package view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import model.entity.Agent;

public class MainWindow extends JFrame {

	private JPanel contentPane;
	private int mapSize;
	private int Aloc;
	private int Bloc;
	private ArrayList<Agent> agents;
	private int cellSize;
	private int HQsize;
	private int agentSize;
	private HashMap<Integer, int[]> map;
	private List sortedKeys;
	private JPanel panel;
	private int high;
	private int width;

	//default
	public MainWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 200, 900, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setBackground(Color.GRAY);
		contentPane.setLayout(null);
		
		contentPane.addComponentListener(new ComponentAdapter() {
		    @Override
		    public void componentResized(ComponentEvent e)
		    {
		    	contentPane.repaint();
		    }
		});
		
		panel = new JPanel() {
            public void paintComponent( Graphics g ) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D)g;
                /*
                for(int i =0; i<10; i++){
	                Line2D line = new Line2D.Double(25, 25, 25, 400);
	                g2.setColor(Color.black);
	                g2.setStroke(new BasicStroke(10));
	                g2.draw(line);
            	}
            	*/
             }
		};
		panel.setBounds(10, 11, 520, 520);
		contentPane.add(panel);
		panel.setBackground(Color.BLUE);
		panel.setLayout(null);
		
		//_-------------------------------__
		this.mapSize = 5;
		this.cellSize = 500/mapSize;
		this.HQsize = this.cellSize;
		this.high = panel.getHeight();
		this.width = panel.getWidth();
		
		this.map = new HashMap<Integer, int[]>();
		
		/*sortedKeys=new ArrayList(map.keySet());
		Collections.sort(sortedKeys);*/
	}
	
	public void paint(Graphics g) {
		//drawLines(g);
		//drawHQs(g);
		
		generatePos();
		cells();
		//printMap();
		//drawRoad(g);
		
	}
	
	/*
	//by data
	public MainWindow(Map m) {
			
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setBounds(100, 100, 610, 488);
			contentPane = new JPanel();
			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			setContentPane(contentPane);
			contentPane.setLayout(null);
	}
	*/
	
	public void generatePos(){
		int cellNum = 0;
		int row = -1;
		int column = -1;

		for(int i=0; i<mapSize; i++){
			row++;
			for(int j=0; j<mapSize; j++){
				column++;
				cellNum++;

				map.put( cellNum, new int[] {row*cellSize, column*cellSize} );
			}
			column = -1;
		}

	}

	public void cells(){
		Iterator iterator = map.keySet().iterator();

		while (iterator.hasNext()) {  
			Integer key = (Integer) iterator.next();
			int[] value = map.get(key);  

			//System.out.println(key + "   " + value[0] +" "+ value[1]);
			if(key==12){
				JButton b = new JButton( "A" );
				b.setBackground(Color.GREEN);
				b.setBounds(value[1], value[0], cellSize/2, cellSize/2);
				panel.add(b);
			}
			
			if(key==5){
				JButton b = new JButton( "B" );
				b.setBackground(Color.RED);
				b.setBounds(value[1], value[0], cellSize/2, cellSize/2);
				panel.add(b);
				
			}
			
			if(key==3){
				JButton b = new JButton( "T" );
				b.setBackground(Color.YELLOW);
				b.setToolTipText("J 12 16 36");
				b.setBounds(value[1], value[0], cellSize/2, cellSize/2);
				panel.add(b);
				
			}
			
			else{
				JButton b = new JButton( key.toString() );
				b.setBounds(value[1], value[0], cellSize/2, cellSize/2);
				panel.add(b);
			}
			
		}  
		panel.repaint();

	}

	public void drawRoad(Graphics g){
		Graphics2D g2 = (Graphics2D) g;
		int size = map.size();
		int cnt = 0;
		
		
		Iterator iterator = map.keySet().iterator();  

		while (iterator.hasNext()) {
			cnt++;
			Integer key = (Integer) iterator.next();
			int[] value = map.get(key);

			if( cnt<size ){
				Integer key2 = (Integer) iterator.next();
				int[] value2 = map.get(key2);

				Line2D lin = new Line2D.Float(value[1], value[0], value2[1], value2[0]);
				g2.draw(lin);
			}
		}  

	}
	
	public void printMap(){
		//System.out.println(map);
		Iterator iterator = map.keySet().iterator();  
		   
		while (iterator.hasNext()) {  
		   Integer key = (Integer) iterator.next();
		   int[] value = map.get(key);  
		   
		   System.out.println(key + "   " + value[1] +" "+ value[0]);  
		}  
		
		
	}
	
	
	
	public void drawLines(Graphics g){
		for (int i = 0; i<mapSize; i++){
			Graphics2D g2 = (Graphics2D) g;

			//fuggoleges
			Line2D lin = new Line2D.Float(10+(i*cellSize), 0, 10+(i*cellSize), 500);
			g2.draw(lin);

			//vizszintes
			Line2D lin2 = new Line2D.Float(0, 10+(i*cellSize), 500, 10+(i*cellSize));
			g2.draw(lin2);
		}
	}
	
	public void drawHQs(Graphics g){
		g.setColor(Color.red);
		g.drawRect (25, 25, HQsize, HQsize);
		g.fillRect(25, 25, HQsize, HQsize);
		//g.fillOval(10, 10, 200, 200);
	}
}//class end
